package logic

import (
	"context"
	"doctor/models/mysql"
	"fmt"
	"gorm.io/gorm"

	"doctor/doctorrpc/doctorrpc"
	"doctor/doctorrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type DoctorUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDoctorUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DoctorUpdateLogic {
	return &DoctorUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DoctorUpdateLogic) DoctorUpdate(in *doctorrpc.DoctorUpdateRequest) (*doctorrpc.DoctorUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var doctors mysql.Doctor
	mysql.DB.Where("id = ?", in.Id).Find(&doctors)
	doctor := mysql.Doctor{
		Model:     gorm.Model{ID: uint(in.Id)},
		Name:      in.Name,
		Headimage: in.Headimage,
		Hospital:  in.Hospatil,
		Rooms:     in.Rooms,
		Desc:      in.Desc,
	}
	err := mysql.DB.Debug().Updates(&doctor).Error
	if err != nil {
		return nil, fmt.Errorf("修改失败")
	}

	return &doctorrpc.DoctorUpdateResponse{
		Id: int64(doctor.ID),
	}, nil
}
